package com.richie696.plugin;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}),
        @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
})
public class CustomPlugin implements Interceptor {

    /**
     * 拦截器的标识方法
     *
     * @param invocation 被拦截的对象信息
     * @return 返回当前拦截器后续操作的对象
     * @throws Throwable 当获取后续拦截器对象出错时抛出该异常
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("CustomPlugin.intercept() output ==> " + invocation);
        return invocation.proceed();
    }

    /**
     * 插件的主要功能方法
     *
     * @param target 插件对象
     * @return 返回插件包对象
     */
    @Override
    public Object plugin(Object target) {
        Object wrap = Plugin.wrap(target, this);
        System.out.println("CustomPlugin.plugin() output ==> " + wrap);
        return wrap;
    }

    /**
     * 获取配置文件参数的方法
     *
     * @param properties 配置文件的参数对象
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("CustomPlugin.setProperties() output ==> " + properties.get("name"));
    }
}
